package net.gz01.wallet.web.controller.admin;

import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jxls.common.Context;

import com.jfinal.plugin.activerecord.Page;

import net.gz01.util.ZODate;
import net.gz01.util.ZOExcel;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.config.SystemEnum;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.TradeOrder;
import net.gz01.wallet.web.model.YouliAccount;

public class YouliAccountOrderController extends BaseController {
    public void search(){
        List<YouliAccount> youliAccountList = YouliAccount.dao.selectAll();

        setAttr("youliAccountList", youliAccountList);
        render("clearing/youliaccount/accountOrderList.html");
    }

    public void listJson() {
        ZOSearchCondition sc = super.getSearchCondition();
        sc.put("orderStatus", SystemEnum.OrderStatus.Success.getValue());
        sc.put("bizType", SystemEnum.BizType.Charge.getValue());
        sc.put("bizType2", SystemEnum.BizType.DF_Personal.getValue());
        sc.put("bizType3",SystemEnum.BizType.DF_Company.getValue());
        sc.put("bizType4", SystemEnum.BizType.Withdraw_Personal.getValue());
        sc.put("bizType5", SystemEnum.BizType.Withdraw_Company.getValue());
        Page<TradeOrder> pageResult = TradeOrder.dao.searchOrderYouliAccount(sc);
        PageRespon pageRespon = new PageRespon(pageResult);
        pageRespon.setTotalInfo(TradeOrder.dao.selectTotalInfo(sc));
        renderJson(pageRespon);
    }

    public void report() throws Exception{
        ZOSearchCondition sc = super.getSearchCondition();
        sc.put("orderStatus", SystemEnum.OrderStatus.Success.getValue());
        sc.put("bizType", SystemEnum.BizType.Charge.getValue());
        sc.put("bizType2", SystemEnum.BizType.DF_Personal.getValue());
        sc.put("bizType3",SystemEnum.BizType.DF_Company.getValue());
        sc.put("bizType4", SystemEnum.BizType.Withdraw_Personal.getValue());
        sc.put("bizType5", SystemEnum.BizType.Withdraw_Company.getValue());
        Page<TradeOrder> pageResult = TradeOrder.dao.searchOrderYouliAccount(sc);

        String fileName = "youliAccount" + ZODate.getDateTime().replace(":", "-");
        String templatePath = SystemEnum.FilePath.ReportTemplate.getValue() + "youliAccount_details.xlsx";
        String targetPath = SystemEnum.FilePath.ReportTmp.getValue() + fileName + ".xlsx";

        LinkedHashMap<String, Context> sheetNameAndData = new LinkedHashMap<>();
        String[] templateSheetName = new String[]{"Sheet1"};
        List<TradeOrder> tradeOrderList= pageResult.getList();
        String name = sc.getStr("youliaccount");
        if(StringUtils.isEmpty(name)){
            name = "ALL";
        }

        Context context = new Context();
        context.putVar("list", tradeOrderList);
        sheetNameAndData.put(name, context);
        ZOExcel.exportLargeData(templatePath, targetPath, sheetNameAndData, templateSheetName);

        renderSuccess("/upload/report/tmp/" + fileName + ".xlsx");
    }



}
